//
//  GPSViewController.m
//  MoneyLog
//
//  Created by Max on 11-4-9.
//  Copyright 2011年 wicresoft. All rights reserved.
//

#import "GPSViewController.h"
#import <CoreLocation/CoreLocation.h>

@implementation GPSViewController
@synthesize locationManager;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    locationManager = [[CLLocationManager alloc] init];
    
    // 如果可以利用本地服务时
    if([CLLocationManager locationServicesEnabled]){
        // 接收事件的实例
        locationManager.delegate = self;
        // 发生事件的的最小距离间隔（缺省是不指定）
        locationManager.distanceFilter = kCLDistanceFilterNone;
        // 精度 (缺省是Best)
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 开始测量
        [locationManager startUpdatingLocation];
    }

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

// 如果GPS测量成果以下的函数被调用
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    
    // 取得经纬度
    CLLocationCoordinate2D coordinate = newLocation.coordinate;
    
   
    CLLocationDegrees latitude = coordinate.latitude; 
    #pragma unused(latitude)
    CLLocationDegrees longitude = coordinate.longitude;
    #pragma unused(longitude)
    // 取得精度
    CLLocationAccuracy horizontal = newLocation.horizontalAccuracy;
    #pragma unused(horizontal)
    CLLocationAccuracy vertical = newLocation.verticalAccuracy;
    #pragma unused(vertical)
    // 取得高度
    CLLocationDistance altitude = newLocation.altitude;
    #pragma unused(altitude)
    // 取得时刻
    NSDate *timestamp = [newLocation timestamp];
    #pragma unused(timestamp)
    
    // 以下面的格式输出 format: <latitude>, <longitude>> +/- <accuracy>m @ <date-time>
    NSLog(@"%@",[newLocation description]);
    
    // 与上次测量地点的间隔距离
    if(oldLocation != nil){
        CLLocationDistance d = [newLocation distanceFromLocation:oldLocation];
        NSLog(@"%@",[NSString stringWithFormat:@"%f", d]);
    }
}

// 如果GPS测量失败了，下面的函数被调用
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"%@",[error localizedDescription]);
}


@end
